﻿using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Mes.Pendant.ArrivalNotices;
using Mes.Pendant.ArrivalNotices.Dto;
using Mes.Pendant.MaterialManagementService.Dto;
using Mes.Pendant.MaterialManagementService.Services;
using Mes.Pendant.MaterialTypeService.Dto;
using Mes.Pendant.MaterialTypeService.Services;
using Mes.Pendant.ProductionWorkOrderService;
using Mes.Pendant.ProductionWorkOrderService.Dto;
using Mes.Pendant.WorkshopSetupService;
using Mes.Pendant.WorkshopSetupService.Dto;
using Microsoft.AspNetCore.Mvc;
using Microsoft.VisualBasic;

namespace Mes.Pendant.Web.Controllers
{
    public class PuffsController : Controller
    {
        private readonly IMaterialTypeServices materialTypeServices;
        private readonly IMaterialManagementServices materialManagementServices;
        private readonly IWorkshopSetupServices workshopSetupServices;
        private readonly IArrivalNoticeServices arrivalNoticeServices;
        private readonly IProductionWorkOrderService productionWorkOrderServices;
        public PuffsController(IMaterialTypeServices materialTypeServices, IMaterialManagementServices materialManagementServices, IWorkshopSetupServices workshopSetupServices, IArrivalNoticeServices arrivalNoticeServices, IProductionWorkOrderService productionWorkOrderServices)
        {
            this.materialTypeServices = materialTypeServices;
            this.materialManagementServices = materialManagementServices;
            this.workshopSetupServices = workshopSetupServices;
            this.arrivalNoticeServices = arrivalNoticeServices;
            this.productionWorkOrderServices = productionWorkOrderServices;
        }

        /// <summary>
        /// 物料产品管理显示
        /// </summary>
        /// <returns></returns>
        public IActionResult test()
        {
            return View();
        }

        /// <summary>
        /// 物料产品管理显示
        /// </summary>
        /// <returns></returns>
        public IActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 新增物料产品
        /// </summary>
        /// <returns></returns>
        public IActionResult AddMaterialView()
        {
            return View();
        }
        /// <summary>
        /// 修改物料产品
        /// </summary>
        /// <returns></returns>
        public IActionResult EditMaterialView()
        {
            return View();
        }
        /// <summary>
        /// 新增物料类型
        /// </summary>
        /// <param name="insertMaterialTypedto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<JsonResult> MaterialtypeInsert(InsertMaterialTypedto insertMaterialTypedto)
        {
            var result = await materialTypeServices.AddMaterialtype(insertMaterialTypedto);
            return Json(new { code = 0, msg = "success", data = result });
        }
        /// <summary>
        /// 获取树形
        /// </summary>
        /// <param name="orderby"></param>
        /// <returns></returns>
        [HttpGet]
        public JsonResult GetMatertypeTrees(int orderby = 0)
        {
            var list = materialTypeServices.GetMaterialtypeTreedtoList(orderby);
            return Json(new { data = list });
        }
        /// <summary>
        /// 根据ID获取物料类型信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetmaterialByid(int id)
        {
            var list = materialTypeServices.GetMaterialtypeById(id);
            return Json(list);
        }
        /// <summary>
        /// 获取物料数据
        /// </summary>
        /// <param name="materialCode"></param>
        /// <param name="materialName"></param>
        /// <param name="index"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<JsonResult> GetMaterialList(string? materialCode, string? materialName, int id = 0, int index = 1, int limit = 10)
        {
            string ids = await GetnumArr(id);
            var materialList = await materialManagementServices.GetSearchMaterialManagements(ids, materialCode, materialName, index, limit);
            return Json(new { data = materialList.Queryable, count = materialList.RowCount, code = 0, msg = "" });
        }
        /// <summary>
        /// 添加物料产品
        /// </summary>
        /// <param name="insertMaterialMangementdto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<int> AddMatermangements(InsertMaterialMangementdto insertMaterialMangementdto)
        {
            var materialdto =await materialManagementServices.AddMaterialmanges(insertMaterialMangementdto);
            return materialdto;
        }
        /// <summary>
        /// 删除物料产品
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<int> DeleteMatermangements(int id)
        {
            var delematerialdto =await materialManagementServices.DeleteMaterialmanges(id);
            return delematerialdto;
        }
        /// <summary>
        /// 点击树形节点获取节点id和子集id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<string> GetnumArr(int id)
        {
            string res = "";
            if (id > 0)
            {
                res = await materialTypeServices.GetNumarr(id);
            }
            return res;
        }






        //-------------------------------------**车间设置******************************************************
        /// <summary>
        /// 显示车间设置信息
        /// </summary>
        /// <returns></returns>
        public IActionResult WorksetupsList()
        {
            return View();
        }
        /// <summary>
        /// 添加车间设置
        /// </summary>
        /// <returns></returns>
        public IActionResult AddWorkView()
        {
            return View();
        }
        /// <summary>
        /// 编辑车间设置
        /// </summary>
        /// <returns></returns>
        public IActionResult EditWorksetup(int id)
        {
            ViewBag.id = id;
            return View();
        }
        /// <summary>
        /// 获取车间设置信息
        /// </summary>
        /// <param name="workshopCode"></param>
        /// <param name="workshopName"></param>
        /// <param name="index"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<JsonResult> SearchWorksetups(string? workshopCode, string? workshopName, int index = 1, int limit = 10)
        {
            var workList =await workshopSetupServices.GetWorksetups(workshopCode, workshopName, index, limit);
            return Json(new { code = 0, msg = "success", data = workList.Queryable, count = workList.RowCount });
        }
        /// <summary>
        /// 添加车间设置信息
        /// </summary>
        /// <param name="insertWorksetupdto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<int> AddWorkdatas(InsertWorksetupdto insertWorksetupdto)
        {
            var result = await workshopSetupServices.AddWorksetup(insertWorksetupdto);
            return result;
        }
        /// <summary>
        /// 删除车间设置
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult DelWorksetdats(int id)
        {
            var result = workshopSetupServices.DeleteWorksetups(id);
            return Json(new { code = 0, msg = "success", data = result });
        }
        /// <summary>
        /// 根据id获取车间设置信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public JsonResult SelectByidWork(int id)
        {
            var list = workshopSetupServices.SelectByWorkId(id);
            return Json(new { code = 0, msg = "success", data = list });
        }
        /// <summary>
        /// 修改车间设置
        /// </summary>
        /// <param name="selectIdWorkdto"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult UpdateWorkdatas(SelectIdWorkdto selectIdWorkdto)
        {
            var entity = workshopSetupServices.UpdateWorksets(selectIdWorkdto);
            return Json(new { code = 0, msg = "success", data = entity });
        }




        //--------------------------到货通知**************************************************************
        /// <summary>
        /// 到货通知页面
        /// </summary>
        /// <returns></returns>
        public IActionResult ArrivalView()
        {
            return View();
        }
        /// <summary>
        /// 添加到货通知页面
        /// </summary>
        /// <returns></returns>
        public IActionResult Addarrivaldatas()
        {
            return View();
        }
        /// <summary>
        /// 修改到货通知页面
        /// </summary>
        /// <returns></returns>
        public IActionResult EditAddivaldatas(int id)
        {
            ViewBag.id = id;
            return View();
        }
        /// <summary>
        /// 获取到货通知数据
        /// </summary>
        /// <param name="inboundListCode"></param>
        /// <param name="notifiName"></param>
        /// <param name="index"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<JsonResult> GetArrialsdata(string? inboundListCode, string? notifiName, int index = 1, int limit = 10)
        {
            var arrilalList = await arrivalNoticeServices.GetArrivalDatas(inboundListCode, notifiName, index, limit);
            return Json(new { code = 0, msg = "success", data = arrilalList.Queryable, count = arrilalList.RowCount });
        }
        /// <summary>
        /// 获取供应商数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<JsonResult> GetSupperdatas()
        {
            var supperList = await arrivalNoticeServices.GetSupperlist();
            return Json(new { code = 0, message = "success", data = supperList });
        }
        /// <summary>
        /// 添加到货通知
        /// </summary>
        /// <param name="notifidatadto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<int> AddNotidatas(InsertNotifidatadto notifidatadto)
        {
            var notifidto =await arrivalNoticeServices.AddNotifsals(notifidatadto);
            return notifidto;
        }
        /// <summary>
        /// 删除到货通知
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<int> DeleteNotifidatadto(int id)
        {
            var delenotifidto =await arrivalNoticeServices.DeleteNotifidatas(id);
            return delenotifidto;
        }
        /// <summary>
        /// 通过id反填到货通知
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<JsonResult> GetByidNotifs(int id)
        {
            var notifsdto =await arrivalNoticeServices.SelectByNotis(id);
            return Json(new { code = 0, msg = "success", data = notifsdto });
        }
        /// <summary>
        /// 修改到货通知
        /// </summary>
        /// <param name="notifidatadto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<int> UpdateArrivdatas(UpdateNotifdatadto notifidatadto)
        {
            var uptnotifidto =await arrivalNoticeServices.UpdateArrivals(notifidatadto);
            return uptnotifidto;
        }



        //---------------------------生产工单***********************************************************
        /// <summary>
        /// 生产工单显示
        /// </summary>
        /// <returns></returns>
        public IActionResult ProductionWorkOrderList()
        {
            return View();
        }
        /// <summary>
        /// 添加生产工单
        /// </summary>
        /// <returns></returns>
        public IActionResult AddProductView()
        {
            return View();
        }
        /// <summary>
        /// 添加生产工单
        /// </summary>
        /// <param name="insertProductdto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<int> AddProductdatas(InsertProductdto insertProductdto)
        {
            var result = await productionWorkOrderServices.AddProducorders(insertProductdto);
            return result;
        }
        /// <summary>
        /// 获取生产工单数据
        /// </summary>
        /// <param name="workOrderCode"></param>
        /// <param name="workOrderName"></param>
        /// <param name="index"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<JsonResult> GetProductdatas(string? workOrderCode, string? workOrderName, int index = 1, int limit = 10)
        {
            var workList = await productionWorkOrderServices.GetProductdatas(workOrderCode, workOrderName, index, limit);
            return Json(new { code = 0, msg = "success", data = workList.Queryable, count = workList.RowCount });
        }
        /// <summary>
        /// 获取物料数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<JsonResult> GetMartelcodes()
        {
            var list = await productionWorkOrderServices.GetMaterials();
            return Json(new { code = 0, msg = "success", data = list });
        }
        /// <summary>
        /// 根据物料编号获取数据
        /// </summary>
        /// <param name="materialCode"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<JsonResult> GetByidMaterials(string materialCode)
        {
            var list = await productionWorkOrderServices.GetMaterialMangement(materialCode);
            return Json(new { code = 0, message = "success", data = list });
        }
    }
}
